package com.ly.system.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.system.system.bean.entity.SysMessageTemplate;
import com.ly.system.mapper.SysMessageTemplateMapper;
import com.ly.system.service.ISysMessageTemplateService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 消息模板
 *
 * @author jeecg
 * @Date: 2019-04-09
 */
@Service
@RequiredArgsConstructor
public class SysMessageTemplateServiceImpl extends ServiceImpl<SysMessageTemplateMapper, SysMessageTemplate> implements ISysMessageTemplateService {

    private final SysMessageTemplateMapper sysMessageTemplateMapper;

    @Override
    public R getId(String id) {
        SysMessageTemplate entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(SysMessageTemplate entity) {
        List<SysMessageTemplate> list = this.list();
        return R.ok(list);
    }

    @Override
    public R getPage(SysMessageTemplate entity, Page<SysMessageTemplate> page) {
        IPage<SysMessageTemplate> pageList = this.page(
                page,
                new LambdaQueryWrapper<SysMessageTemplate>()
                        .eq(StrUtil.isNotEmpty(entity.getTemplateType()), SysMessageTemplate::getTemplateType, entity.getTemplateType())
                        .like(StrUtil.isNotBlank(entity.getTemplateCode()), SysMessageTemplate::getTemplateCode, entity.getTemplateCode())
                        .like(StrUtil.isNotBlank(entity.getTemplateName()), SysMessageTemplate::getTemplateName, entity.getTemplateName())
                        .orderByDesc(SysMessageTemplate::getCreateTime)
        );
        return R.ok(pageList);
    }

    @Override
    public R add(SysMessageTemplate entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(SysMessageTemplate entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<SysMessageTemplate> dto) {
        List<SysMessageTemplate> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public SysMessageTemplate getTemplateByCode(String code) {
        return this.getOne(
                new LambdaQueryWrapper<SysMessageTemplate>()
                        .eq(SysMessageTemplate::getTemplateCode,code)
        );
    }
}
